package com.shuai.config;

import com.shuai.utils.filter.CheckTokenFilter;
import com.shuai.utils.hander.securityHander.CustomAccessDeineHandler;
import com.shuai.utils.hander.securityHander.CustomizeAuthenticationEntryPoint;
import com.shuai.utils.hander.securityHander.MyAuthenticationFailureHandler;
import com.shuai.utils.hander.securityHander.MyAuthenticationSuccessHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;
import org.springframework.web.cors.CorsUtils;

import javax.annotation.Resource;

/**
 * SecurityConfig
 *
 * @author shuai
 * @since 2022
 */
@Slf4j
@EnableWebSecurity
@Configuration
/** 控制权限注解 */
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private MyAuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Resource
    private MyAuthenticationFailureHandler myAuthenticationFailureHandler;

    @Resource
    private CustomAccessDeineHandler customAccessDeineHandler;

    @Resource
    private CustomizeAuthenticationEntryPoint customizeAuthenticationEntryPoint;

    @Resource
    private CheckTokenFilter checkTokenFilter;

    //允许不规范 URL 访问

    @Bean
    public HttpFirewall httpFirewall() {
        return new DefaultHttpFirewall();
    }

    /**
     * 密码加密验证
     *
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().headers().frameOptions().disable();//支持跨域
        http.addFilterBefore(checkTokenFilter, UsernamePasswordAuthenticationFilter.class);
        http.formLogin()
                //登录地址
                .loginProcessingUrl("/auth/login")
                //登录成功处理器
                .successHandler(myAuthenticationSuccessHandler)
                //登录失败处理器
                .failureHandler(myAuthenticationFailureHandler)
                //关闭CSRF
                .and().csrf().disable()
                //设置拦截信息
                .authorizeRequests()
                //作用是将PreflightRequest不做拦截
                .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
                .anyRequest().authenticated()
                .and()
                //授权失败处理器
                .exceptionHandling()
                //匿名用户无权限访问资源
                .authenticationEntryPoint(customizeAuthenticationEntryPoint)
                //解决认证过的用户访问无权限资源时的异常
                .accessDeniedHandler(customAccessDeineHandler);
    }
}
